<?php

namespace app\admin\controller;

use app\admin\validate\SDistribute as SDistributeValidate;
use app\common\business\SDistribute as SDistributeBus;
use app\common\lib\toCamelCase;

class SDistribute extends AuthBase
{
    /**************** 分发平台管理 **********************/
    public function distributeList(){
        $where = [];
        // 获取分页参数
        $page = $this->request->param('pageNum', 1);
        $limit = $this->request->param('pageSize', 10);
        $distributeName = $this->request->get("distributeName","");
        if ($distributeName) {
            $where[] = ['distribute_name', 'like', '%'.$distributeName.'%'];
        }
        $where[] = ["del_flag", "=", '0'];
        try {
            $menuData = (new SDistributeBus())->getNormalDistribute($where,'*',$limit,$page);
            $menuData = (new toCamelCase())->toCamelCaseArray($menuData);
        }catch (\Exception $e){
            $menuData = [];
        }
        return show(config("status.success"),"分发平台列表",$menuData);
    }

    //分发平台添加
    public function distributeAdd(){
        if (!$this->request->isPost()){
            return show(config("status.error"),"请求方式错误");
        }
        //参数校验
        $allParams = request()->param();
        $data = (new toCamelCase())->toCamelCaseArray($allParams,'1',2);
        $validate = new SDistributeValidate();
        if(!$validate->check($data)){
            return show(config("status.error"),$validate->getError());
        }
        try {
            $result = (new SDistributeBus())->addDistribute($data);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        return show(config("status.success"),"新增成功",$result);
    }

    //分发平台修改
    public function distributeEdit(){
        $allParams = request()->param();
        $data = (new toCamelCase)->toCamelCaseArray($allParams,1,2);
        try {
            $result = (new SDistributeBus())->updateDistributeById($data['distribute_id'],$data);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        return show(config("status.success"),"修改成功",$result);
    }

    //分发平台详情
    public function distributeDetails(){
        $distribute = $this->request->param("distribute","","trim");
        if(empty($distribute)){
            return show(config("status.error"),"参数不能为空");
        }
        try {
            $configData = (new SDistributeBus())->getDistributeById($distribute);
            $configData = (new toCamelCase())->toCamelCaseArray($configData);
        }catch (\Exception $e){
            $configData = [];
        }
        return show(config("status.success"),"分发平台详情",$configData);
    }

    //分发平台删除
    public function distributeDel(){
        $distribute = $this->request->param("distribute","","trim");
        if(empty($distribute)){
            return show(config("status.error"),"参数不能为空");
        }
        try {
            $result = (new SDistributeBus())->delDistributeIds($distribute);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        return show(config("status.success"),"删除成功",$result);
    }
}
